package io.shuttle.mbe.api

import io.shuttle.mbe.api.annotation.ChromeMinVersion
import io.shuttle.mbe.api.annotation.ChromeOSOnly
import io.shuttle.mbe.api.annotation.PromiseStyleMinVersion
import io.shuttle.mbe.core.Promise

////////////////////
// Printing Metrics
////////////////////
/**
 * Use the `Browser.printingMetrics` API to fetch data about printing usage.
 *
 * Permissions: "printingMetrics"
 *
 * Note: Only available to policy installed extensions.
 * @platform ChromeOS only
 * @since Chrome 79
 */
@ChromeOSOnly
@ChromeMinVersion(79)
interface PrintingMetrics {
    // Returns the list of the finished print jobs.
    @PromiseStyleMinVersion(96)
    fun getPrintJobs(
        callback: io.shuttle.mbe.api.types.Value1Function<List<PrintJobInfo>>? = null
    ): Promise<List<PrintJobInfo>>

    // Event fired when the print job is finished.
    // This includes any of termination statuses: FAILED, CANCELED and PRINTED.
    val onPrintJobFinished: Events.Event<io.shuttle.mbe.api.types.Value1Function<PrintJobInfo>>?

    enum class ColorMode {
        // Specifies that black and white mode was used.
        BLACK_AND_WHITE,

        // Specifies that color mode was used.
        COLOR
    }

    enum class DuplexMode {
        // Specifies that one-sided printing was used.
        ONE_SIDED,

        // Specifies that two-sided printing was used, flipping on long edge.
        TWO_SIDED_LONG_EDGE,

        // Specifies that two-sided printing was used, flipping on short edge.
        TWO_SIDED_SHORT_EDGE
    }

    data class MediaSize(
        // Height (in micrometers) of the media used for printing.
        var height: Number,
        // Vendor-provided ID, e.g. "iso_a3_297x420mm" or "na_index-3x5_3x5in".
        // Possible values are values of "media" IPP attribute and can be found on IANA page .
        var vendorId: String,
        // Width (in micrometers) of the media used for printing.
        var width: Number
    )

    data class Printer(
        // Displayed name of the printer.
        var name: String,
        // The source of the printer.
        var source: PrinterSource,
        // The full path for the printer. Contains protocol, hostname, port, and queue.
        var uri: String,
    )

    enum class PrinterSource {
        // Specifies that the printer was added by user.
        USER,

        // Specifies that the printer was added via policy.
        POLICY,
    }

    data class PrintJobInfo(
        // The job completion time (in milliseconds past the Unix epoch).
        var completionTime: Number,
        // The job creation time (in milliseconds past the Unix epoch).
        var creationTime: Number,
        // The ID of the job.
        var id: String,
        // The number of pages in the document.
        var numberOfPages: Number,
        // The info about the printer which printed the document.
        var printer: Printer,
        @ChromeMinVersion(85)
        var printer_status: Printing.PrinterStatus,
        // The settings of the print job.
        var settings: PrintSettings,
        // Source showing who initiated the print job.
        var source: PrintJobSource,
        // ID of source. Null if source is PRINT_PREVIEW or ANDROID_APP.
        var sourceId: String?,
        // The final status of the job.
        var status: PrintJobSource,
        // The title of the document which was printed.
        var title: String,
    )

    enum class PrintJobSource {
        // Specifies that the job was created from the Print Preview page initiated by the user.
        PRINT_PREVIEW,

        // Specifies that the job was created from an Android App.
        ANDROID_APP,

        // Specifies that the job was created by extension via Chrome API.
        EXTENSION,

        // Specifies that the job was created by an Isolated Web App via API.
        ISOLATED_WEB_APP
    }

    enum class PrintJobStatus {
        // Specifies that the print job was interrupted due to some error.
        FAILED,

        // Specifies that the print job was canceled by the user or via API.
        CANCELED,

        // Specifies that the print job was printed without any errors.
        PRINTED
    }

    data class PrintSettings(
        // The requested color mode.
        var color: ColorMode,
        // The requested number of copies.
        var copies: Number,
        // The requested duplex mode.
        var duplex: DuplexMode,
        // The requested media size.
        var mediaSize: MediaSize
    )
}